package com.xiaotu.spider.iqy.pageprocessor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.HotTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 IQYHotPageProcessor
 * @日期 2017年2月13日
 * @作者 高海军
 * @功能 爱奇艺网站热剧爬取
 */
@Component("IQYHotPageProcessor")
@Scope("prototype")
public class IQYHotPageProcessor extends AUrlPageProcessor
{
	
	private String inlandHot = "http://list.iqiyi.com/www/2/15-------------11-%s-1-iqiyi--.html"; //内地热门
	
	private String inlandRecentUpd = "http://list.iqiyi.com/www/2/15-------------4-%s-1-iqiyi--.html";//内地最近更新
	
	private String hongKongHot = "http://list.iqiyi.com/www/2/16-------------11-%s-1-iqiyi--.html";//香港热门
	
	private String hongKongRecentUpd = "http://list.iqiyi.com/www/2/16-------------4-%s-1-iqiyi--.html";//香港最新更新
	
	private String taiWanHot = "http://list.iqiyi.com/www/2/1117-------------11-%s-1-iqiyi--.html";//台湾热门
	
	private String taiWansRecentUpd = "http://list.iqiyi.com/www/2/1117-------------4-%s-1-iqiyi--.html";//台湾最近更新
	
    @Override
    protected String[] getStartRequestUrls()
    {
        return new String[]{
        		String.format(inlandHot, 1),
        		String.format(inlandRecentUpd, 1),
        		String.format(hongKongHot, 1),
        		String.format(hongKongRecentUpd, 1),
        		String.format(taiWanHot, 1),
        		String.format(taiWansRecentUpd, 1)
        		};
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.IQIYI_HOT;
    }

    @Override
    public String getPageDataType(Page page)
    {
        return Constants.PageDataType.SEARCH_LIST;
    }

    @Override
    public void process(Page page)
    {
        
    	String currUrl = page.getRequest().getUrl();
    	
    	System.out.println("当前页数："+currUrl);
    	
    	Integer currPage = page.getRequest().getExtras().get("currPage")==null?1:Integer.parseInt(page.getRequest().getExtras().get("currPage").toString());
    	
    	currPage = currPage+1;
    	
    	Map<String,Object> param = new HashMap<String,Object>();
    	
    	if(currUrl.contains("list.iqiyi.com/www/2/15-------------11")){
    		param.put("reqUrl", inlandHot);
    	}else if(currUrl.contains("list.iqiyi.com/www/2/15-------------4")){
    		param.put("reqUrl", inlandRecentUpd);
    	}else if(currUrl.contains("list.iqiyi.com/www/2/16-------------11")){
    		param.put("reqUrl", hongKongHot);
    	}else if(currUrl.contains("list.iqiyi.com/www/2/16-------------4")){
    		param.put("reqUrl", hongKongRecentUpd);
    	}else if(currUrl.contains("list.iqiyi.com/www/2/1117-------------11")){
    		param.put("reqUrl", taiWanHot);
    	}else if(currUrl.contains("list.iqiyi.com/www/2/1117-------------4")){
    		param.put("reqUrl", taiWansRecentUpd);
    	}
    	
    	
    	
    	param.put("currPage", currPage);
    	
    	page.getRequest().setExtras(param);
    	
    	List<String> divList = page.getHtml()
                .xpath("//div[@class='qy-list-wrap']/ul/li").all();
        if (divList == null || divList.isEmpty())
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);

        List<HotTVModel> tvList = new ArrayList<HotTVModel>();
        for (String divStr : divList)
        {
            Html divObj = new Html(divStr);
            tvList.add(new HotTVModel(Constants.SiteName.IQIYI,
                    divObj.xpath(
                            "//div[@class='title-wrap ']//p[@class='main']//a/text()")
                            .get(),
                    Arrays.asList(divObj.xpath(
                            "//div[@class='title-wrap ']//p[@class='sub']//text()").replace("主演:", "").replace(" ", "").get().trim().split("/"))
                            ,
                    divObj.xpath("//div[@class='title-wrap ']//p[@class='main']/a/@href")
                            .get()));
        }
        page.putField(HotTVModel.class.getName(), tvList);
        
        if (PropertiesUtil.getPropertyByInt(
                Constants.SpiderConfigKey.HOTTV_PAGE_SIZE) >= currPage)
            PageUtils.addTargetRequest(page,
            		String.format(page.getRequest().getExtras().get("reqUrl").toString(), currPage),
                    page.getRequest().getExtras());
        
    }
}
